package com.spark.modules.generation.controller;

import com.spark.common.core.Result;
import com.spark.modules.generation.service.IGenCodeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 代码生成Controller
 *
 * @author LYCHEE
 * @date 2025/2/28 14:27
 */
@RestController
@RequestMapping(value = "/gen/code")
public class GenCodeController {

    @Resource
    private IGenCodeService genCodeService;

    /**
     * 预览代码
     *
     * @param modelCode 模型编码
     * @return 数据
     */
    @GetMapping(value = "/previewCode")
    public Result<?> previewCode(@RequestParam String modelCode) {
        return Result.ok(genCodeService.previewCode(modelCode));
    }

    /**
     * 生成后端代码到项目
     *
     * @param modelCode 模型编码
     * @return 数据
     */
    @GetMapping(value = "/genCodeToProject")
    public Result<?> genCodeToProject(@RequestParam String modelCode) {
        return genCodeService.genCodeToProject(modelCode);
    }

    /**
     * 通过页面ID预览前端代码
     *
     * @param pageId 页面ID
     * @return 代码
     */
    @GetMapping(value = "/previewFrontEndCode")
    public Result<?> previewFrontEndCode(@RequestParam String pageId) {
        return genCodeService.previewFrontEndCode(pageId);
    }

    /**
     * 生成前端代码到项目
     *
     * @param pageId 页面ID
     * @return 数据
     */
    @GetMapping(value = "/genCodeFrontEndToProject")
    public Result<?> genCodeFrontEndToProject(@RequestParam String pageId) {
        return genCodeService.genCodeFrontEndToProject(pageId);
    }

    /**
     * 生成菜单SQL
     *
     * @param pageId 页面ID
     * @return 数据
     */
    @GetMapping(value = "/genSql")
    public Result<?> genSql(@RequestParam String pageId) {
        return genCodeService.genSql(pageId);
    }

    /**
     * 下载后端代码
     *
     * @param modelCode 模型编码
     * @param response  响应
     * @throws IOException
     */
    @GetMapping("/downloadBankCode")
    public void downloadBankCode(@RequestParam String modelCode, HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"generated-code.zip\"");
        genCodeService.downloadBankCode(modelCode, response);
        response.getOutputStream().flush();
    }

    /**
     * 下载前端代码
     *
     * @param pageId   页面ID
     * @param response 响应
     * @throws IOException
     */
    @GetMapping("/downloadFrontCode")
    public void downloadFrontCode(@RequestParam String pageId, HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"generated-code.zip\"");
        genCodeService.downloadFrontCode(pageId, response);
        response.getOutputStream().flush();
    }
}
